package com.kehao.leetcode.jzoffer;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/23 14:33
 */
public class JZSolution023 {
    private static class ListNode {
        int val;
        ListNode next;
        ListNode() {
        }
        ListNode(int val) {
            this.val = val;
        }
        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int swap = 0;
        ListNode p1 = headA,p2 = headB;
        while (p1!=p2){
           p1 = p1.next;
           p2 = p2.next;
           if(p1==null) {
               p1 = headB;
               swap++;
           }
           if(p2==null){
               p2 = headA;
               swap++;
           }
           if(swap >=3) return null;
        }
        return p1;
    }
}
